<?php
/**
 * array helper
 *
 * Some additional very useful functions
 *
 * @version:	0.1
 * @package:	core
 * @author:	alex_ez
 * @created:	15 nov 2008
 *
 * @link:	http://radiance-project.com/
 *
 * @copyright:	(c) 2008-2011 Alex Yaroshevich
 */

function array_flat( $A, $tkey, $tvalue = null ) {

	if( !is_array( $A ) ) return null;
	
	$_r = array( );
	
	foreach( $A as $k => $v )
	{
		$__nv = is_array( $tvalue )
			? array_intersect_key( $v, array_flip( array_values( $tvalue ) ) )
			: ( is_null( $tvalue ) ? $v : ( ($tvalue == '$') ? $k : $v[ $tvalue ] ) );
		
		if( isset($v[ $tkey ]) || $tkey=='$' )
		{
			$__nk = ($tkey == '$') ? $k : $v[ $tkey ];
			$_r[$__nk] = $__nv;
		}
		elseif( empty( $tkey ) )
		{
			$_r[] = $__nv;
		}
	}

	return $_r;
}
	
function joinf( $d = null, $a, $t = null )
{
	if( !is_array( $a ) || empty( $a ) ) return 'null';
	
	if( is_null( $d ) || !mb_strlen($d) || !$d ) $d = '; ';
	if( is_null( $t ) || !mb_strlen($t) || !$t ) $t = '%k:%v';
	
	$t = str_replace( array('%k','%v'), array('%1$s','%2$s'), $t );
	$r = '';
	foreach( $a as $k=>$v ) $r .= sprintf( $t, $k, $v ) . $d;
	
	return mb_substr( $r, 0, -mb_strlen( $d ) );
}
	
function array_simplify( $A, $backlevel = 0 ) //Array, parentKey
{
	$r = array( );

	foreach( $A as $k => $v )
	if( is_array( $v ) ) {

		$vbacklevel = 0;
		$vback = $v;
		while( $vback = reset( $vback ) )
		{
			$vbacklevel++;
			//var_dump( $vback, is_array( $vback ), $vbacklevel );
			if( $vbacklevel <= $backlevel ) {
				$r[$k] = $v;
				continue 2;
			}
			if( !is_array( $vback ) ) break;
		}
		
		foreach( self::simplify( $v ) as $_k => $_v )
			$r["{$k}[{$_k}"] = $_v;
	}
	else $r[$k] = $v;

	$rk = array_map( create_function( '$a', '$k=""; $a=explode("[",str_replace("]","",$a)); $k=array_shift($a); if( sizeof($a) ) $k .= "[".join("][",$a)."]"; return $k;' ), array_keys( $r ) );
	$rv = array_values( $r );
	$r = array( );

	if( count( $rk ) && count( $rk ) == count( $rv ) )
		$r = array_combine( $rk, $rv );

	return $r;
}

function array_transpose($array) {
	$t = array();

	foreach($array as $key => $value) {
		foreach($value as $key2 => $value2) {
			$t[$key2][$key] = $value2;
		}
	}

	return $t;
}

function array_remap($A, $map, $strict = false) {
	if(!is_string($map)&&!is_array($map))
		return false;
	
	$r = array();
	
	if(is_string($map))
		$map = array($map => $map);
	
	if(!$strict&&isset($map[0]))
	{
		$rmap = array();
		foreach($map as $k=>$v)
			$rmap[ is_int($k)&&isset($A[$v]) ? $v : $k ] = $v;
		
		$map = $rmap;
	}
	
	foreach($map as $k=>$v)
		$r[$k] = isset($A[$v])?$A[$v]:null;
	
	return $r;
}

function unset_keys($A, $keys) {
	if (!is_array($A))
		return array();

	if (is_string($keys))
		$keys = explode(',',$keys);

	foreach (array_keys($A) as $i)
		foreach ($keys as $k)
			unset($A[$i][$k]);

	return $A;
}


if (!is_callable('array_filter_recursive')) {

function array_filter_recursive($A) {
	if (!is_array($A))
		return $A;
		
	$A = array_filter($A);
	foreach(array_keys($A) as $k) {
		if (empty($A[$k]))
			unset($A[$k]);
		elseif (is_array($A[$k]))
			$A[$k] = array_filter_recursive($A[$k]);
	}
	return array_filter($A);
}

}
